package kiwi.assist.dev.util {
	import fl.managers.StyleManager;
	import fl.core.InvalidationType;
	import fl.core.UIComponent;

	import ghostcat.util.ReflectUtil;
	import ghostcat.util.core.ClassFactory;

	import kiwi.assist.dev.guieditor.model.PropertyConfig;
	import kiwi.assist.dev.guieditor.model.SkinConfig;
	import kiwi.assist.dev.guieditor.model.UiConfig;
	import kiwi.assist.dev.guieditor.model.format.NumericFormat;
	import kiwi.util.bitmap.BitmapPool;

	import flash.display.DisplayObject;
	import flash.events.Event;

	/**
	 * 将一个可见的dos(Skin)包装成一个component
	 * @author zhangming.luo
	 */
	public class SkinComponentAdapter extends UIComponent {
		private var _skin : SkinConfig;
		private var impl : DisplayObject;
		public var uiConfig : UiConfig;
		private var widthProperty : PropertyConfig;
		private var heightProperty : PropertyConfig;
		private var mouseEnbaledProperty : PropertyConfig;
		private var sourceProperty : PropertyConfig;

		public function SkinComponentAdapter(skin : SkinConfig, uiPrefix : String = null) {
			this._skin = skin;
			uiConfig = new UiConfig();
			var implClz : Class = ReflectUtil.getClass(skin.clz);
			if (implClz) {
				impl = ClassFactory.apply(implClz);
				setSize(impl.width, impl.height);
			} else {
				impl = BitmapPool.getBitmap(uiPrefix + "/" + skin.name);
				if (impl.width > 0 && impl.height > 0) {
					setSize(impl.width, impl.height);
				} else {
					impl.addEventListener(Event.RESIZE, function() : void {
						setSize(impl.width, impl.height);
					});
				}
			}
			addChild(impl);
			uiConfig.propertyList.push(widthProperty = new PropertyConfig());
			uiConfig.propertyList.push(heightProperty = new PropertyConfig());
			// if (impl.hasOwnProperty("mouseEnabled")) {
			// uiConfig.propertyList.push(mouseEnbaledProperty = new PropertyConfig());
			// mouseEnbaledProperty.format = new BooleanFormat();
			// mouseEnbaledProperty.value = impl["mouseEnabled"];
			// }
			widthProperty.key = "width";
			widthProperty.format = new NumericFormat();
			heightProperty.key = "height";
			heightProperty.format = new NumericFormat();
			widthProperty.value = impl.width;
			heightProperty.value = impl.height;
		}

		public function get skin() : SkinConfig {
			return _skin;
		}

		override protected function draw() : void {
			if (isInvalid(InvalidationType.SIZE)) {
				impl.width = _width;
				impl.height = _height;
			}
			super.draw();
		}

		public function get dx() : int {
			return this.x;
		}

		public function get dy() : int {
			return Number(StyleManager.getStyle("winHeight")) - y - height;
		}
	}
}
